package com.atguigu.spzx.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.atguigu.spzx.user.demo.HttpUtils;
import com.atguigu.spzx.user.service.SmsService;
import com.atguigu.spzx.utils.SmsUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.http.HttpResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: SmsServiceImpl
 * Package: com.atguigu.spzx.user.service.impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/16 2:56
 * @Version 1.0
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送验证码
     * @param phone
     */
    @Override
    public void sendValidateCode(String phone) {
        // 1. 判断验证码是否过期
        String validateCode = stringRedisTemplate.opsForValue().get("phone:code:" + phone);
        if(!StrUtil.isEmpty(validateCode)) {
            return;
        }
        // 2. 生成验证码
        String validateCodeNew = RandomStringUtils.randomNumeric(4);
        // 3. 保存到redis中
        stringRedisTemplate.opsForValue().set("phone:code:" + phone, validateCodeNew, 60, TimeUnit.SECONDS);
        // 4. 发送验证码
        SmsUtils.sendSMS(validateCodeNew, phone);
    }


}
